<?php
/**
 Template Name:news_list
 */
	$siteurl = get_bloginfo('template_url');
	$selfPageLink = "/all-games" ;
	$pageSize = 50 ;
	$pageModel= 10 ;
	
	//父类型参数（找出子类型参数）
	$cat = $_POST['cat'] ; 
	if($cat == null || "" == $cat){
		$cat = $_GET['cat'] ;
	}
	//$categories = "" ;
	//$catStr = "" ;
	if($cat != null && "" != $cat){
		$category_id = get_cat_ID($cat);
		//$categories = get_categories ('child_of='.$category_id) ;
		//$categories = $wpdb->get_results("select p.name,p.term_id as  pterm_id ,t.name,t.term_id as term_id ".
		//	" from wp_terms t ,wp_term_taxonomy m , wp_terms p ".
		//	" where t.term_id = m.term_id and p.term_id = m.parent and p.term_id  ='".$category_id."'") ;
		//foreach ($categories as $category) {
		//	$catStr =$catStr."'".$category->term_id."'," ;
		//}
	}

//页码参数	
	$paged = $_POST["paged"]; 
	if($paged == null || "" == $paged ){
		$paged = $_GET["paged"]; 
		if($paged == null || "" == $paged ){
			$paged = 0 ;
		}
	}
	$statNum = $pageSize * $paged ;
	get_header();
?>

	<div id="mews_cont_left">
		<div class="menuDiv">
			<img src="<?php echo $siteurl ?>/images/xialajiantou.gif" class="xialajiantou" />
		 	<h5 id="menu_ti" class="paddingleft">游戏列表</h5>	
			<ul>
				<?php 
				if($category_id != null && "" != $category_id){
				?>
					<li class="paddingleft weixuanlink"><a href="<?php echo $selfPageLink ;?>">全部游戏</a></li>
				<?php 
				}else{
				?>
					<li id="newsxuan" class="paddingleft">全部游戏</li>
				<?php 
				}
				?>
				<?php if($category_id == "6"){ ?>
					<li id="newsxuan" class="paddingleft">Flash小游戏</li>
				<?php }else{?>
					<li class="paddingleft weixuanlink"><a href="<?php echo $selfPageLink."/cat_Flash小游戏";?>">Flash小游戏</a></li>
				<?php } if($category_id == "7"){ ?>
					<li id="newsxuan" class="paddingleft">网页游戏</li>
				<?php }else{?>
					<li class="paddingleft weixuanlink"><a href="<?php echo $selfPageLink."/cat_网页游戏".$v->name;?>">网页游戏</a></li>
				<?php } if($category_id == "8"){ ?>
					<li id="newsxuan" class="paddingleft">手机游戏</li>
				<?php }else{?>
					<li class="paddingleft weixuanlink"><a href="<?php echo $selfPageLink."/cat_手机游戏".$v->name;?>">手机游戏</a></li>
				<?php }?>
			</ul>
			
		</div>
	</div>
	<div id="newsontlist">
		<?php 
		
		$condition = " where post.post_status = 'publish' AND post.post_type = 'post' " ;
		if($category_id != null && "" != $category_id){
			$condition=$condition." and post_category_id ='".$category_id."'"  ;
		}
		
		$countQueryStr = " select count(post.Id) as  counts  from wp_posts post ".
			$condition ;	
		$mycounts = $wpdb->get_results($countQueryStr) ;
		$mycount= 0 ;
		if($mycounts != null){
			$mycount = $mycounts[0]->counts ;
		}

		$queryStr = " select 	post.ID as ID,post.comment_count as comment_count,post.post_author as post_author ,date_format(post.post_date,'%Y-%m-%d') as post_date ,". 
			" post.post_title as post_title,post.post_content as post_content ,post.post_game_url GameLink ,post.post_game_image GamePic,post.post_category_name cat_name ".
			" from wp_posts post ".$condition."  order by post.post_date desc  LIMIT ".$statNum.",".$pageSize ;
		$myposts = $wpdb->get_results($queryStr);
		foreach($myposts as $mypost) {
			$gamePic =  $mypost->GamePic ;  
			$gameLink = $mypost->GameLink ;  
			$postLink = get_permalink($mypost->ID);
			$postTitle = $mypost->post_title ; 
			$post_content = mb_strimwidth(strip_tags(apply_filters('the_content', $mypost->post_content)), 0, 200,"…");
			
			$post_author = $mypost->post_author ;
			$comment_count = $mypost->comment_count ;
			$post_date = $mypost->post_date ;
		?>
			<div class="news_contM">
				<h3 class="list_title"><a href="<?php echo $postLink; ?>"><?php echo $postTitle ; ?></a></h3>
				<p><?php echo $post_content; ?></p>
				<span class="list_add">游戏源地址：<samp><?php echo $postLink; ?></samp></span>
				<ul>
					<li class="date">更新时间：<?php echo $post_date ;?></li>
				</ul>
			</div>
		<?php 
		}
		?>
		
		
				<!--  分页开始 -->
				
				<div id="scroll">
					<?php 
					$totalPage = ceil($mycount/$pageSize) ;
					if($totalPage >0){
						$i = 0 ;
						if($cat)
							$pagedLink = $selfPageLink."/cat_".$cat;
						else 
							$pagedLink = $selfPageLink;
						if(intval($paged) > 0 ){		
					?>
							<a href="<?php echo $pagedLink."/p_".(intval($paged)-1); ?>" ><img src="<?php echo $siteurl ?>/images/next-top.gif" alt="上一页" title="上一页" class="f_l"/></a>
					<?php 
						}
					?>
					<?php 
						if($totalPage<=$pageModel){
						 	for(;$i<$totalPage;$i++){
						 		if($i == intval($paged)){
					?>
									<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
						 		}else{
					?>
									<a href="<?php echo $pagedLink."/p_".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
						 		}
						 	}
						 		
						}elseif($totalPage < $pageModel + 3){
							$halfPageModel = intval($pageModel /3)  ;
							if(intval($paged) < $halfPageModel*2){
								
								for($i=0;$i < $halfPageModel*2+1;$i++){
									if($i == intval($paged)){
					?>
										<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
									}else{
					?>
										<a href="<?php echo $pagedLink."/p_".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
									}
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 		
								for($i = $totalPage-($pageModel-$halfPageModel*2 -2);$i<$totalPage;$i++){
					?>
									<a href="<?php echo $pagedLink."/p_".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 				
								}	
					?>
					<?php 
							}elseif(intval($paged) > $halfPageModel - 1) {
								for($i =0 ;$i< $halfPageModel-1;$i++){
					?>
									<a href="<?php echo $pagedLink."&paged=".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 				
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 	
								for($i=$totalPage-($pageModel-$halfPageModel);$i < $totalPage;$i++){
									if($i == intval($paged)){
					?>
										<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
									}else{
					?>
										<a href="<?php echo $pagedLink."/p_".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
									}
								}
							}
						}else{
							$halfPageModel = intval($pageModel /2)  ;
							if(intval($paged) < $halfPageModel-1){
								for($i =0 ;$i< $halfPageModel;$i++){
									if($i == intval($paged)){
					?>
										<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
									}else{
					?>
										<a href="<?php echo $pagedLink."/p_".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
									}
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 			
								for($i =$totalPage - ($pageModel - $halfPageModel - 1) ;$i< $totalPage ;$i++){
					?>
									<a href="<?php echo $pagedLink."/p_".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php				
								}
							}elseif(intval($paged) > $totalPage - ($halfPageModel) ) {
								for($i = 0 ;$i< $halfPageModel-1 ;$i++){
					?>
									<a href="<?php echo $pagedLink."/p_".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php				
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 		
								for($i = $totalPage - ($pageModel - $halfPageModel) ;$i< $totalPage;$i++){
									if($i == intval($paged)){
					?>
										<font class="linknone"><?php echo ($i +1);?></font>
					<?php 
									}else{
					?>
										<a href="<?php echo $pagedLink."/p_".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
									}
								}	
								
							}else {
					?>
								<a href="<?php echo $pagedLink."/p_0"; ?>" class="linkstyle03"><?php echo 1;?></a>
								<a href="<?php echo $pagedLink."/p_1"; ?>" class="linkstyle03"><?php echo 2;?></a>
					<?php 
								$uses = 3 ;
								if($paged > $totalPage/2){
									$uses = 2 ;
					?>
									<a href="<?php echo $pagedLink."/p_1"; ?>" class="linkstyle03"><?php echo 3;?></a>
					<?php				
								}
					?>
								<a class="linkstyle03"><?php echo "...";?></a>
								<a href="<?php echo $pagedLink."/p_".(intval($paged)-1); ?>" class="linkstyle03"><?php echo intval($paged);?></a>
								<font class="linknone"><?php echo (intval($paged) +1);?></font>
								<a href="<?php echo $pagedLink."/p_".(intval($paged)+1); ?>" class="linkstyle03"><?php echo (intval($paged) +2);?></a>
								<a class="linkstyle03"><?php echo "...";?></a>
					<?php 		
								for($i = $totalPage - $uses ;$i< $totalPage;$i++){
					?>
										<a href="<?php echo $pagedLink."/p_".(intval($i)); ?>" class="linkstyle03"><?php echo ($i +1);?></a>
					<?php 
								}
							}
						}						
						if(intval($paged)<$totalPage-1){
					?>
							<a href="<?php echo $pagedLink."/p_".(intval($paged)+1); ?>" ><img src="<?php echo $siteurl ?>/images/next-bottom.gif" alt="下一页" title="下一页" class="f_l" /></a>
					<?php 
						}
					}
					?>
				</div>
				<!-- 分页结束 -->
				
	</div>
	<div id="list_right">
		<!-- 热门游戏开始 -->
			<div id="list_right">
				<h2>热门游戏推荐</h2>
				<?php 
					//query_posts('orderby=comment_count&showposts=4');//返回分类ID为7的2篇内容
					//while (have_posts()) : the_post(); 
					$i =1 ;
					$hotposts = getHotPost(10);
					foreach($hotposts as $hotpost) {
						//$gameSize = get_post_meta($hotpost->ID, 'GameSize', true);  
						//$gamePic = get_post_meta($hotpost->ID, 'GamePic', true);  
						$gamePic = $hotpost->GamePic ;
						//$gameLink = get_post_meta($hotpost->ID, 'GameLink', true);  
						$postLink = get_permalink($hotpost->ID);
				?>
						<div class="list_tuijian">
							<span class="list_img"><a href="<?php echo $postLink; ?>"><img src="<?php echo $gamePic; ?>" width="104" height="79" alt="<?php echo $hotpost->post_title;  ?>" title="<?php echo $hotpost->post_title;  ?>" /></a></span>
							<!-- <span class="listriht listriht_tuijian"><img src="<?php echo $siteurl ?>/images/tuijian/<?php echo  $i++ ;?>.jpg" /></span> -->
							<h5 class="listriht listriht_tit"><a href="<?php echo $postLink; ?>"><?php echo $hotpost->post_title;  ?></a></h5>
							<span class="listriht listriht_cat "><?php echo get_cat_name($hotpost->term_id);?></span>
						</div>
				<?php 
					}
					//endwhile;
				?>
			</div>
			<!-- 热门游戏结束 -->
	</div>
<?php get_footer();?>